package org.kmfdm.horrorscope.tapestry.pages.user;

import java.util.List;




import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.beaneditor.BeanModel;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.BeanModelSource;
import org.kmfdm.horrorscope.core.spring.ApplicationContextProviderSingleton;
import org.kmfdm.horrorscope.metamodel.Astrologer;
import org.kmfdm.horrorscope.metamodel.Customer;
import org.kmfdm.horrorscope.metamodel.User;
import org.kmfdm.horrorscope.metamodel.Chart;
import org.kmfdm.horrorscope.service.CustomerCharts;

import org.springframework.context.ApplicationContext;

public class UserDetails {
	
	@Inject
	private CustomerCharts cc;
	
	@SessionState
	private User user;
	
	@Inject
    private ComponentResources resources;
	
	
	private Astrologer astrologer;
	private Chart chart;
	

	public void setChart(Chart chart) {
		this.chart = chart;
	}



	
	private Customer customer;
	
	public String getUserType(){
		return user.getClass().getSimpleName();
		
	}
	
	public boolean getIsCustomer() {
		if(getUserType().equals("CustomerBean"))
			
		return true;
		return false;
	}

	public boolean getIsAstrologer() {
		if(getUserType().equals("AstrologerBean"))
			return true;
			return false;
	}


	

	public Astrologer getAstrologer() {
		if (getIsAstrologer()){
		 astrologer= (Astrologer) user;
		 
		 return astrologer;}
		return null;
	}

	public Customer getCustomer() {
		if (getIsCustomer()){
			
			//custom
		return customer =(Customer) user;}
		return null;
	}
	
	
	
	@Inject
    private BeanModelSource beanModelSource;


	
	 public BeanModel getModel() {
         BeanModel model = beanModelSource.createDisplayModel(Chart.class,resources.getMessages());
         
         return model;
     }
		
		
	
	public List<Chart> getCharts(){
		
		if (getIsCustomer())
			return cc.customerCharts(getCustomer());
			return null;
		
	}
	public Chart getChart() {
		if(chart==null){
			chart=(Chart) applicationContext().getBean(Chart.class.getName());
		}return chart;
		
	}


	private ApplicationContext applicationContext(){
		return new ApplicationContextProviderSingleton().getContext();
	}
}
